<?php
/**
 * 轻舟CMS(QZLOG)
 *
 * 一款基于PHP+MYSQL的开源内容管理系统，模板标签简单、可扩展性强，利于二次开发，轻松建站的首选利器。
 *
 * @package     QZLOG
 * @license     http://www.qzlog.com/license
 * @link        http://www.qzlog.com
 * @mail        info@shyohan.com
 * @since       Version 3.0
 * @copyright   Copyright (c) 2015 - 2017 上海优翰信息科技有限公司. All rights reserved.
 */

defined('IN_QZLOG') OR exit('QZLOG:No direct script access allowed!');

/**
 * 模板管理控制器
 * @author LINK <link@shyohan.com>
 */
class ThemeController extends QzController
{
    /**
     * 模板列表
     * @author LINK <link@shyohan.com>
     */
    public function index()
    {
        $style = array();
        $dirs = Dir::tree('Template');
        foreach ($dirs as $tpl) {
            $xml = $tpl['path'] . '/config.xml';
            if (!is_file($xml)) {
                continue;
            }
            if (!$config = Xml::toArray(file_get_contents($xml))) {
                continue;
            }
            $tpl['name'] = isset($config['name']) ? $config['name'][0] : ''; //模型名
            $tpl['author'] = isset($config['author']) ? $config['author'][0] : ''; //作者
            $tpl['image'] = isset($config['image']) ? __ROOT__ . '/Template/' . $tpl['filename'] . '/' . $config['image'][0] : __CONTROLLER_TPL__ . '/img/preview.jpg'; //预览图
            $tpl['email'] = isset($config['email']) ? $config['email'][0] : ''; //邮箱
            $tpl['current'] = C("WEB_STYLE") == $tpl['filename'] ? 1 : 0; //正在使用的模板
            $style[] = $tpl;
        }
        $bread = make_bread(array('模板管理' => '', '模板列表' => ''));
        $this->assign('bread', $bread);
        $this->assign('style', $style);
        $this->display();
    }

    /**
     * 创建模板
     * @author LINK <link@shyohan.com>
     */
    public function add()
    {
        if (IS_POST) {
            $data = $_POST;
            //模板名首字母大写
            $data['name'] = ucfirst($data['name']);
            //验证模板目录
            if (!is_writable('Template/')) {
                qz_ajax_return_error('Template/ 不可写');
            }
            //-------------------模板目录----------------------
            $templateDir =  'Template/' . $data['name'] . '/';
            if (!dir::create($templateDir)) {
                qz_ajax_return_error('模板目录创建失败');
            }
            //-------------------主题图片----------------------
            copy('Static/Images/nopic.gif', $templateDir .'preview.jpg');
            $viewQzXml = <<<qzXml
<?xml version="1.0" encoding="UTF-8"?>
<root>
<name>{$data['name']}</name>
<author>{$data['author']}</author>
<image>preview.jpg</image>
<email>{$data['email']}</email>
</root>
qzXml;
            file_put_contents($templateDir . 'config.xml', $viewQzXml);
            //------------------index.html---------------------
            $viewIndex = <<<qzIndex
<html>
    <head>
        <title>403 Forbidden</title>
    </head>
    <body>
        <p>Directory access is forbidden.</p>
    </body>
</html>
qzIndex;
            file_put_contents($templateDir . 'index.html', $viewIndex);
            qz_ajax_return('创建成功，请刷新后台,然后去'.$templateDir.'目录下编辑你的模板吧!');
        } else {
            $this->display();
        }
    }

    /**
     * 选择模板
     * @author LINK <link@shyohan.com>
     */
    public function selectStyle()
    {
        $dir_name = Q("dirName");
        if ($dir_name) {
            import('Config.Model.ConfigModel');
            $Model = K("Config");
            $Model->where("name='WEB_STYLE'")->save(array("value" => $dir_name));
            //更新配置文件
            $Model->updateCache();
            //删除前台编译文件
            is_dir("./temp/qzlog/Content/Compile") and Dir::del("./temp/qzlog/Content/Compile");
            //删除编译文件
            is_dir('temp/QZLOG/Index') and dir::del('temp/QZLOG/Index');
            $this->success('操作成功');
        }
    }

}